package com.yanghx.user.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yanghx.service.dto.OrderDto;
import com.yanghx.user.dao.CustomerRepository;
import com.yanghx.user.domain.Customer;
import com.yanghx.user.feign.OrderClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangHX
 * createTime  2019/8/9 16:34
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerResource {

    @Resource
    private CustomerRepository customerRepository;
    @Resource
    private OrderClient orderClient;


    @PostConstruct
    public void init() {
        Customer customer = new Customer();
        customer.setUserName("imooc");
        customer.setPassword("111111");
        customer.setRole("User");
        customerRepository.save(customer);
    }


    @PostMapping
    public Customer create(@RequestBody Customer customer) {
        return customerRepository.save(customer);
    }

    @GetMapping
    @HystrixCommand
    public List<Customer> getAll() {
        return customerRepository.findAll();
    }

    @GetMapping(value = "/my")
    @HystrixCommand
    public Map getMyInfo() {
        Customer customer = customerRepository.findOneByUserName("imooc");
        OrderDto myOrder = orderClient.getMyOrder(1L);
        HashMap<String, Object> map = new HashMap<>();
        map.put("customer", customer);
        map.put("order", myOrder);
        return map;
    }

}
